package com.dlc.yunguang.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.FrameLayout;
import butterknife.BindView;
import butterknife.OnClick;
import com.dlc.commonlibrary.ble.UHFR2000Helper;
import com.dlc.commonlibrary.dialog.DoResultDialog;
import com.dlc.commonlibrary.dialog.ReadNumsDialog;
import com.dlc.commonlibrary.utils.LogPlus;
import com.dlc.commonlibrary.widget.TitleBar;
import com.dlc.yunguang.base.BaseActivity;
import com.dlc.yunguang.R;
import com.seuic.uhf.EPC;

import java.util.ArrayList;
import java.util.List;

/**
 * 标签管理
 */
public class TagManageActivity extends BaseActivity {

    public static final String EXTRA_RFIDS = "rfids";

    @BindView(R.id.title_bar) TitleBar mTitleBar;
    @BindView(R.id.item_unbind) FrameLayout mItemUnbind;
    @BindView(R.id.item_bind) FrameLayout mItemBind;
    @BindView(R.id.item_scrap) FrameLayout mItemScrap;
    private Handler mHandler;
    private DoResultDialog doResultDialog;
    private ReadNumsDialog doReadDialog;
    private StringBuilder mStringBuilder;
    private  Class<? extends Activity> mClass;
    private boolean readState= false;;

//    private UHFService mDevice;
//    private List<EPC> mEPCList;
//    private InventoryRunable mInventoryRunable;
//    public boolean mInventoryStart = false;
//    private Thread mInventoryThread;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mTitleBar.leftExit(this);
//        mDevice = UHFService.getInstance();
//        mEPCList = new ArrayList<EPC>();
//        mInventoryRunable = new InventoryRunable();
        mHandler = new Handler();
        doReadRFID();
        doResult();


    }

    @Override
    protected int getLayoutID() {
        return R.layout.activity_tag_manage;
    }

    @OnClick({ R.id.item_unbind, R.id.item_bind, R.id.item_scrap })
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.item_unbind:
                getData();
                mClass = UnbindTagActivity.class;

                break;
            case R.id.item_bind:
                getData();
                mClass = BindTagActivity.class;
               // test();
                break;
            case R.id.item_scrap:
                getData();
                mClass = BucaoScrapActivity.class;


                break;
        }
    }
    private void test(){
        ArrayList<String> mList = new ArrayList<>();
        mList.add("0002DEF60017"); mList.add("0002DEF60018");
        Intent intent = new Intent(getActivity(), mClass);
        intent.putExtra(EXTRA_RFIDS, mList);
        startActivity(intent);
    }

    private void getData(){
        readState= true;
        doReadDialog.showWorking(true);
        UHFR2000Helper.getInstance().readData(new UHFR2000Helper.BleCallBack() {
            @Override
            public void readDatas(List<EPC> data) {
                LogPlus.e("mEPCList="+data.toString());
                if (data.size() > 0){
                    ArrayList<String> mList = new ArrayList<>();
                    String str = "";
                    for (EPC epc : data) {
                        str = epc.getId();
                        if (str.length() > 14) {
                            str = str.substring(str.length() - 12, str.length());
                            mList.add(str);
                        }
                    }
                    LogPlus.e("mList="+mList.toString());
                    Intent intent = new Intent(getActivity(), mClass);
                    intent.putExtra(EXTRA_RFIDS, mList);
                    startActivity(intent);
                }else {
                    doReadDialog.dismiss();
                    doResultDialog.setFailedText("读取不到RFID");
                    doResultDialog.setFailedImage(R.mipmap.ic_error);
                    doResultDialog.showResultAndDismiss(false, 2000);

                }
            }

            @Override
            public void readNums(final int nums) {
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (readState) {
                            doReadDialog.showWorking(nums);
                        }
                    }
                });

            }
        });
    }

//    private class InventoryRunable implements Runnable {
//        @Override
//        public void run() {
//
//            while (mInventoryStart) {
//                mEPCList = mDevice.getTagIDs();
//                LogPlus.e("mEPCList="+mEPCList.toString());
//                try {
//                    Thread.sleep(100);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//
//            }
//
//        }
//    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private void doReadRFID() {
        doReadDialog =
                ReadNumsDialog.newDialog(this)
                        .setSuccessImage(R.mipmap.ic_success)
                        .setSuccessText(getString(R.string.scan_over))
                        .setButton("完成扫描", new ReadNumsDialog.OnClickButtonListener() {
                            @Override
                            public void onClickButton(ReadNumsDialog dialog) {
                                UHFR2000Helper.getInstance().stopRead();
                                readState= false;
                                doReadDialog.dismiss();
                                doResultDialog.showResultAndDismiss(true, 1000);
                            }
                        });

        doReadDialog.setCancelable(false);

    }
    private void doResult() {

        doResultDialog = DoResultDialog.newDialog(this)
                .setWorkingText(getString(R.string.commit_counting_result))
                .setSuccessImage(R.mipmap.ic_success)
                .setSuccessText(getString(R.string.commit_counting_result_success));

        doResultDialog.setCancelable(false);

    }


}
